<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Image Controller Test</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script src="assets/prototype.js" type="text/javascript"></script>
  <script src="assets/unittest.js" type="text/javascript"></script>
  
  <script src="../../public/javascripts/controllers/image_controller.js" type="text/javascript"></script>
  
  <link rel="stylesheet" href="assets/unittest.css" type="text/css" />
</head>
<body>

<div>
	These are DOM that the test can act on. (aka. Mock DOM)
	<div id="image_controller_space"></div>
	<a href="#" id="image_link_1">Image Link 1</a>
	<a href="#" id="image_link_2">Image Link 1</a>
	<a href="#" id="image_link_210">Image Link 2</a>
	<a href="#" id="image_link_16">Image Link 3</a>
	<a href="#" id="image_link_17">Image Link 17</a>
	<a href="#" id="image_link_15">Image Link 15</a>
	<a href="#" id="image_link_209">Image Link 209</a>
	<form id="form_selected_ids" onsubmit="return false">
	<input id="selected_ids" name="selected_ids" type="hidden" value='' />
	</form>
</div>

<div id="content">

  <div id="header">
    <h1>Image Controller Test</h1>
    <p>
      This file tests <strong>image_controller.js</strong>. Image Controller controls batch processing of images. It allows users to quickly
      and efficiently select multiple images/pictures for processing in batch mode.
    </p>
  </div>

  <!-- Log output -->
  <div id="testlog"> </div>

</div>

<script type="text/javascript">
// <![CDATA[

  new Test.Unit.Runner({
    
    // replace this with your real tests
    
    setup: function() {
      image_controller = new ImageController;
    },
    
    teardown: function() {
      
    },
    
    testTruth: function() { with(this) {
      assert(true);
    }},
    
    testNewImageController: function() { with(this) {
      assertNotNull(image_controller.space);
    }},
    
    testInitialBatchSize: function() { with(this) {
      assertEqual(0, image_controller.batch_size());
    }},
    
    testBatchInitiallyHidden: function() { with(this) {
      assertNotVisible(image_controller.space);
    }},
    
    testAddBatchIncreaseBatchSize: function() { with(this) {
      initial_size = image_controller.batch_size();
      image_controller.add_batch(1)
      assertEqual(initial_size+1, image_controller.batch_size());
    }},
    
    testAddBatch: function() { with(this) {
      assertEqual(false, image_controller.is_in_batch(15));
      image_controller.add_batch(15);
      assertEqual(true, image_controller.is_in_batch(15));
    }},
    
    testAddBatchDoesNotMakeItVisible: function() { with(this) {
      assertEqual(0, image_controller.batch_size());
      image_controller.add_batch(1);
      assertNotVisible(image_controller.space);
    }},
    
    testAddBatchMultiple: function() { with(this) {
      assertEqual(false, image_controller.is_in_batch(15));
      image_controller.add_batch(15);
      assertEqual(false, image_controller.is_in_batch(17));
      assertEqual(true, image_controller.is_in_batch(15));
      image_controller.add_batch(17);
      assertEqual(true, image_controller.is_in_batch(17));
    }},
    
    testAddBatchTwice: function() { with(this) {
      image_controller.add_batch(15);
      image_controller.add_batch(15);
      assertEqual(true, image_controller.is_in_batch(15));
      assertEqual(1, image_controller.batch_size());
    }},
    
    testRemoveBatch: function() { with(this) {
      image_controller.add_batch(209);
      assertEqual(true, image_controller.is_in_batch(209));
      image_controller.remove_batch(209);
      assertEqual(0, image_controller.batch_size());
      assertEqual(false, image_controller.is_in_batch(209));
    }},
    
    testRemoveEmptyBatch: function() { with(this) {
      assertEqual(0, image_controller.batch_size());
      image_controller.remove_batch(209);
      //resilient and not throwing anything
      //only check to make sure size of batch is still right
      assertEqual(0, image_controller.batch_size());
      
    }},
    
    testRemoveLastElementDoesNotAffectBatchVisibility: function() { with(this){
      assertNotVisible(image_controller.space);
      image_controller.add_batch(1);
      assertNotVisible(image_controller.space);
      image_controller.remove_batch(1);
      assertNotVisible(image_controller.space);
    }},
    
    testRemoveNotFoundElement: function() { with(this) {
      assertEqual(false, image_controller.is_in_batch(210));
      var initial_size = image_controller.batch_size();
      image_controller.remove_batch(210);
      //size should not change if removing element not in batch
      assertEqual(initial_size, image_controller.batch_size());
    }},
    
    testClearBatch: function() { with(this) {
      image_controller.add_batch(15);
      image_controller.add_batch(16);
      image_controller.add_batch(17);
      assertEqual(3, image_controller.batch_size());
      image_controller.clear_batch();
      assertEqual(0, image_controller.batch_size());
      assertEqual(false, image_controller.is_in_batch(15));
      assertEqual(false, image_controller.is_in_batch(16));
      assertEqual(false, image_controller.is_in_batch(17));
      assertEqual(false, image_controller.is_in_batch(20));
    }},
    
    testClearBatchMakeItInvisible: function() { with(this) {
      image_controller.add_batch(15);
      assertNotVisible(image_controller.space);
      image_controller.clear_batch();
      assertNotVisible(image_controller.space);
    }},
    
/*    testEndBatchMode: function() { with(this) {
      image_controller.end_batch_mode();
      initial_size = image_controller.batch_size();
      assertVisible(image_controller.space);
      assertEqual(initial_size, image_controller.batch_size());
    }},
*/
	testAddElementToBatchChangeLinkClass: function() { with(this) {
      image_controller.add_batch(1);
      assertVisible($('image_link_1'))
      assertNotNull($('image_link_1').className.match('clicked'))
    }},
    
    testRemoveElementFromBatchChangeLinkClass: function() { with(this) {
      image_controller.add_batch(2);
      image_controller.add_batch(1);
      assertEqual(true, image_controller.is_in_batch(2));
      image_controller.remove_batch(2);
      assertNull($('image_link_2').className.match('clicked'))
      assertNotNull($('image_link_1').className.match('clicked'))
    }},
    
    testToggleBatchAnElement: function() { with(this) {
      image_controller.toggle_batch(2);
      assertEqual(true, image_controller.is_in_batch(2));
      assertEqual(1, image_controller.batch_size());
      image_controller.toggle_batch(2);
      assertEqual(false, image_controller.is_in_batch(2));
      assertEqual(0, image_controller.batch_size());
    }},
    
    testInputFieldExists: function() { with(this) {
      assertNotNull(image_controller.input_field);
    }},
    
    testInputFieldIntiallyEmpty: function() { with(this) {
      assertEqual('', $F(image_controller.input_field))
    }},
    
    testAddElementsUpdateField: function() { with(this) {
      image_controller.add_batch(1);
      assert($F('selected_ids').match('1'))
      image_controller.add_batch(2);
      assert($F('selected_ids').match('2'))
    }},
    
    testRemoveElementsUpdateField: function() { with(this) {
      image_controller.add_batch(1);
      assert($F('selected_ids').match('1'))
      image_controller.add_batch(2);
      assert($F('selected_ids').match('1'))
      assert($F('selected_ids').match('2'))
      image_controller.remove_batch(1);
      assertNull($F('selected_ids').match('1'))
      assert($F('selected_ids').match('2'))
    }},
    
    testUpdateSelectedElementsClassNameFromField: function() { with(this) {
      $('selected_ids').value = '1,2,15,16';
      image_controller.update_from_field();
      assertEqual('1,2,15,16',$F('selected_ids'));
      [1,2,15,16].each(function(el){
      	assert(image_controller.is_in_batch(el));
  	  })
    }},
    
    testClearBatchUpdateClassName: function() { with(this) {
      image_controller.add_batch(1);
      image_controller.add_batch(15);
      image_controller.add_batch(2);
      image_controller.add_batch(16);
      assertEqual('1,15,2,16', $F(image_controller.input_field))
      image_controller.clear_batch();
      [1,2,15,16].each(function(el){
      	assertNull($('image_link_'+el).className.match('clicked'))
  	  })
    }},
    
  }, "testlog");
// ]]>
</script>
</body>
</html>